<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Auth;
use Validator;
use Hash;
class HomePost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::guard('admin')->check();
    }
    /**
     * 验证原密码是否正确
     *
     * @return array
     */
    public function boot()
    {
        Validator::extend('check_password', function ($attribute, $value, $parameters, $validator) {
            return Hash::check($value,Auth::guard('admin')->user()->password);
        });
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $this->boot();
        return [
            'original_password' => 'sometimes|required|check_password',//原密码 check_password-》boot()验证
            'password' => 'sometimes|required|confirmed',//新密码
            'password_confirmation' => 'sometimes|required',//确认密码
        ];
    }
    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'password.required' => '新密码不能为空！',
            'original_password.required' => '原密码不能为空！',
            'password_confirmation.required'  => '确认密码不能为空！',
            'password.confirmed'  => '两次密码输入不一致！',//两次密码对比
            'original_password.check_password'  => '原密码不正确！',
        ];
    }
}
